package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.YouWorldGiftCardDAO;
import com.ecoolex.discount.sales.core.external.pojo.YouWorldCatalogsResult.Card;
import com.ecoolex.discount.sales.core.external.pojo.YouWorldCatalogsResult.FixedCard;
import com.ecoolex.discount.sales.entity.YouWorldGiftCard;
import com.ecoolex.discount.sales.entity.YouWorldGiftCard.Column;
import com.ecoolex.discount.sales.entity.YouWorldGiftCardExample;
import com.ecoolex.discount.sales.entity.YouWorldGiftCardExample.Criteria;
import com.ecoolex.discount.sales.enums.YouWorldGiftCardStatus;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.Check;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import static com.ecoolex.discount.sales.entity.YouWorldGiftCard.NOT_DELETED;

@Service
public class YouWorldGiftCardManager {
    @Autowired
    private YouWorldGiftCardDAO dao;

    public Pagination<YouWorldGiftCard> query(String cardName,Sorting[] sorts, Page page) {
        YouWorldGiftCardExample example = new YouWorldGiftCardExample();
        Criteria criteria = example.createCriteria();
        if(Check.notNull(cardName)) {
        	criteria.andCardNameLike("%"+cardName+"%");
        }
        criteria.andDeletedEqualTo(YouWorldGiftCard.NOT_DELETED);
        example.orderBy(Sortings.builder(sorts).toSql());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        return Pagination.create(page, dao.countByExample(example), ()-> dao.selectByExample(example));
    }

    public YouWorldGiftCard getByCardId(Integer cardId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(cardId, NOT_DELETED);
    }

    public List<YouWorldGiftCard> queryAll() {
        YouWorldGiftCardExample example = new YouWorldGiftCardExample();
        example.createCriteria()
                .andDeletedEqualTo(NOT_DELETED);
        return dao.selectByExample(example);
    }

    private void verify() {
        // TODO 验证参数
    }

    private void create(YouWorldGiftCard record, String createUser) {
        record.setIsAdded(false);
        record.setStatus(YouWorldGiftCardStatus.NORMAL.real());
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    private void update(YouWorldGiftCard record, String updateUser) {
        record.setStatus(YouWorldGiftCardStatus.NORMAL.real());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.excludes(Column.isAdded,
                Column.deleted,
                Column.createUser,
                Column.createTime));
    }

    private YouWorldGiftCard setting(Card card) {
        YouWorldGiftCard record = new YouWorldGiftCard();
        record.setCardId(card.getCard_id());
        record.setStorefrontStatus(card.getStorefront_status());
        record.setOutOfStockEndTime(card.getOut_of_stock_end_time());
        record.setCardName(card.getCard_name());
        record.setCardDiscountPercent(card.getCard_discount_percent());
        record.setBrandLogo(card.getBrand_logo());
        record.setCardImage(card.getCard_image());
        record.setCardValueType(card.getCard_value_type());
        record.setCardMinValue(card.getCard_min_value());
        record.setCardMaxValue(card.getCard_max_value());
        record.setRanking(card.getRanking());
        record.setFaceValue(null);
        return record;
    }

    private YouWorldGiftCard setting(Card card, FixedCard fixedCard) {
        YouWorldGiftCard record = new YouWorldGiftCard();
        record.setCardId(fixedCard.getCard_id());
        record.setStorefrontStatus(card.getStorefront_status());
        record.setOutOfStockEndTime(card.getOut_of_stock_end_time());
        record.setCardName(card.getCard_name());
        record.setCardDiscountPercent(card.getCard_discount_percent());
        record.setBrandLogo(card.getBrand_logo());
        record.setCardImage(card.getCard_image());
        record.setCardValueType(card.getCard_value_type());
        record.setCardMinValue(null);
        record.setCardMaxValue(null);
        record.setRanking(card.getRanking());
        record.setFaceValue(fixedCard.getFace_value());
        return record;
    }

    public void create(Card card, String createUser) {
        create(setting(card), createUser);
    }

    public void create(Card card, FixedCard fixedCard, String createUser) {
        create(setting(card, fixedCard), createUser);
    }

    public void update(YouWorldGiftCard oldCard, Card card, String updateUser) {
        update(setting(card), updateUser);
    }

    public void update(YouWorldGiftCard oldCard, Card card, FixedCard fixedCard, String updateUser) {
        update(setting(card, fixedCard), updateUser);
    }

    public void setNotFoundSyncData(YouWorldGiftCard card, String updateUser) {
        card.setStatus(YouWorldGiftCardStatus.NOT_FOUND_SYNC_DATA.real());
        card.setUpdateUser(updateUser);
        card.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(card,
                Column.status,
                Column.updateUser,
                Column.updateTime);
    }

    public void setAdded(YouWorldGiftCard card, String updateUser) {
        card.setIsAdded(true);
        card.setUpdateUser(updateUser);
        card.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(card,
                Column.isAdded,
                Column.updateUser,
                Column.updateTime);
    }

}